package ui.slider
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import gs.TweenLite;
	import gs.easing.Expo;

	
	public class SliderView extends Sprite
	{
		//START.
		
		private var sliderBg:DisplayObject;
		private var sliderBar:DisplayObject;
		private var minSize:Number;
		
		private var contentSize:Number = 0;
		private var pageSize:Number = 0;
		
		private const HIDE_DURATION:Number = 2.0;
		
		public var sliderBarIsHidden:Boolean;
		
		//CONSTRUCTOR.
		public function SliderView()
		{
			initialize();
		}
		
		//METHODS.
		
		// initialize.
		private function initialize():void
		{
			sliderBar = getChildByName("bar");
			sliderBg = getChildByName("bg");
			sliderBar.visible = false;

			minSize = sliderBar.height;

			var size:Number = Math.max(this.width,this.height);
			scaleX = 1;
			scaleY = 1;
			sliderBg.height = size;
		}
		
		
		public function setSize(contentSize:Number,pageSize:Number):void
		{
			this.contentSize = contentSize;
			this.pageSize = pageSize;
			
			if(contentSize <= pageSize) 
			{
				sliderBar.visible = false;
				sliderBar.y = 0;
				return;
			}
			
			sliderBar.height = Math.max(minSize, sliderBg.height * (pageSize / contentSize));
			sliderBar.visible = true;
		}
		
		public function update(position:Number,animated:Boolean = false):void
		{
			if(contentSize <= pageSize)
				return;
			
			var sliderY:Number = (position / (contentSize - pageSize)) * (sliderBg.height - sliderBar.height);
			sliderY = Math.min(sliderBg.height - sliderBar.height,sliderY);
			sliderY = Math.max(0,sliderY);
			
			if(animated)
				TweenLite.to(sliderBar,1,{y:sliderY,ease:Expo.easeOut});
			else
				sliderBar.y = sliderY; 	
		}
		
		
		public function showSlider(animated:Boolean = false):void
		{
			sliderBarIsHidden = false;
			TweenLite.killTweensOf(this);
			
			if(!animated) 
			{
				visible = true;
				alpha = 1;
				return;
			}
			
			visible = true;
			TweenLite.to(this,HIDE_DURATION,{alpha:1,ease:Expo.easeInOut});
		}
		
		public function hideSlider(animated:Boolean = false):void
		{
			sliderBarIsHidden = true;
			TweenLite.killTweensOf(this);
			if(!animated) 
			{
				visible = false;
				alpha = 0;
				return;
			}
			TweenLite.to(this,HIDE_DURATION,{alpha:0,ease:Expo.easeInOut,onComplete:function():void{this.visible = false;}});
		}
		
		
		// set size.
		public function setSliderSize(value:Number):void
		{
			sliderBg.height = value;
			setSize(contentSize,pageSize);
		}
		
		
		//END.
	}
}